package com.jobsleaf.filter.subnetwork.tree;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.part.ViewPart;

import com.jobsleaf.tree.domain.Gsm;
import com.jobsleaf.tree.domain.Lac;
import com.jobsleaf.tree.domain.Rnc;
import com.jobsleaf.tree.domain.Subnetwork;
import com.jobsleaf.tree.domain.Utran;

public class SubnetworkView extends ViewPart
{
	private SubnetworkLabelProvider mLabelProvider;
	private CheckboxTreeViewer mTreeViewer;
	private Combo mSubnetWorkCombo;
	private SubnetworkFilter mFilter;

	public void createPartControl(Composite parent)
	{
		parent.setLayout(new GridLayout(1, false));
		mSubnetWorkCombo = new Combo(parent, SWT.READ_ONLY);
		mSubnetWorkCombo.setLayoutData(new GridData(GridData.FILL,
				GridData.FILL, true, false));
		mTreeViewer = new CheckboxTreeViewer(parent);
		mTreeViewer.setContentProvider(new SubnetworkContentProvider());
		mLabelProvider = new SubnetworkLabelProvider();
		mTreeViewer.setLabelProvider(mLabelProvider);
		Tree tree = mTreeViewer.getTree();
		tree.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true,
				true));
		mTreeViewer.setInput(SubnetworkUtility.getInput());

		for (Subnetwork subnetwork : SubnetworkUtility.getParentList()) {
			mSubnetWorkCombo.add(subnetwork.getName());
		}

		// Add the Filter to the tree Viewer
		mFilter = new SubnetworkFilter();
		mTreeViewer.addFilter(mFilter);

		addListener();
	}

	private void addListener()
	{
		mSubnetWorkCombo.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				mFilter.setSearchText(((Combo) e.widget).getText());
				mTreeViewer.refresh();
			}
		});

		mTreeViewer.addCheckStateListener(new ICheckStateListener()
		{
			public void checkStateChanged(CheckStateChangedEvent event)
			{
				ITreeContentProvider contentProvider = (ITreeContentProvider) mTreeViewer
				.getContentProvider();

				Object obj = event.getElement();
				if (obj instanceof Subnetwork) {
					boolean currentState = mTreeViewer.getChecked(obj);
					mTreeViewer.setSubtreeChecked(obj, false);
					mTreeViewer.setChecked(event.getElement(), currentState);

				} else if (obj instanceof Lac || obj instanceof Rnc) {
					obj = contentProvider.getParent(event.getElement());
					if (mTreeViewer.getChecked(obj)) {
						mTreeViewer.setChecked(obj, false);
					} else {
						obj = contentProvider.getParent(obj);
						if (mTreeViewer.getChecked(obj)) {
							mTreeViewer.setChecked(obj, false);
							// treeViewer.setChecked(event.getElement(), false);
						} else {
							obj = contentProvider.getParent(event.getElement());
							mTreeViewer.setChecked(obj, false);

						}
					}
				} else if (obj instanceof Utran || obj instanceof Gsm) {
					obj = contentProvider.getParent(event.getElement());

					if (mTreeViewer.getChecked(obj)) {
						mTreeViewer.setChecked(obj, false);
						// mTreeViewer(event.getElement(), false);
					} else {
						boolean currentState = mTreeViewer.getChecked(event
								.getElement());
						mTreeViewer.setSubtreeChecked(event.getElement(), false);
						mTreeViewer.setChecked(event.getElement(), currentState);

					}

				}

			}
		});
	}

	public void setFocus()
	{
	}
}
